package org.bangumibuddy.exception;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import lombok.extern.slf4j.Slf4j;
import org.bangumibuddy.exception.rss.RssSubscribedException;
import org.bangumibuddy.pojo.ResponseResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.MalformedURLException;
import java.nio.file.AccessDeniedException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(UsernameNotFoundException.class)
    public ResponseResult<?> handleUsernameNotFoundException(UsernameNotFoundException e) {
        return ResponseResult.error(e.getMessage());
    }

    @ExceptionHandler(JWTVerificationException.class)
    public ResponseResult<?> handleTokenAuthenticationFailureException(JWTVerificationException e) {
        e.printStackTrace();
        return ResponseResult.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult<?> handleException(Exception e) {
        log.error("Exception:{}", e.getMessage());
        e.printStackTrace();
        return ResponseResult.error("未知错误");
    }

    @ExceptionHandler(TokenExpiredException.class)
    public ResponseResult<?> handleExpiredTokenException(TokenExpiredException e) {
        log.error("TokenExpiredException:{}", e.getMessage());
        e.printStackTrace();
        return ResponseResult.error("token已过期");
    }

    @ExceptionHandler(PasswordErrorException.class)
    public ResponseResult<?> handlePasswordErrorException(PasswordErrorException e) {
        e.printStackTrace();
        return ResponseResult.error(e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public ResponseResult<?> handleRuntimeException(RuntimeException e) {
        log.error("RuntimeException:{}", e.getMessage());
        e.printStackTrace();
        return ResponseResult.error(e.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public ResponseResult<?> handleAccessDeniedException(AccessDeniedException e) {
        log.error("AccessDeniedException:{}", e.getMessage());
        e.printStackTrace();
        return ResponseResult.tokenerror(e.getMessage());
    }

    @ExceptionHandler(AuthenticationException.class)
    public ResponseResult<?> handleAuthenticationException(AuthenticationException e) {
        log.error("AuthenticationException:{}", e.getMessage());
        e.printStackTrace();
        return ResponseResult.tokenerror("token失效，请重新登录");
    }

    @ExceptionHandler(DataBaseQueryException.class)
    public ResponseResult<?> handleDataBaseQueryException(DataBaseQueryException e) {
        log.error("DataBaseQueryException:{}", e.getMessage());
        e.printStackTrace();
        return ResponseResult.error("数据查询出错！");
    }

    @ExceptionHandler(MalformedURLException.class)
    public ResponseResult<?> handleMalformedURLException(MalformedURLException e) {
        log.error("MalformedURLException:{}", e.getMessage());
        e.printStackTrace();
        return ResponseResult.error("URL格式错误！" + e.getMessage());
    }

    @ExceptionHandler(RssSubscribedException.class)
    public ResponseResult<?> handleRssSubscribedException(RssSubscribedException e) {
        log.error("RssSubscribedException:{}", e.getMessage());
        e.printStackTrace();
        return ResponseResult.error("RSS订阅错误：" + e.getMessage());
    }
}
